package com.navinfo.platform.trip.common.arithmetic;

import com.navinfo.platform.trip.common.arithmetic.data.CommonData;
import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.var.VarRangeTorque;
import com.navinfo.platform.trip.common.arithmetic.enums.RangeTorqueEnum;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.util.ArithmeticUtils;

import java.util.Optional;

/**
 * 发动机扭矩区间算法<br>
 * 依赖{@link IdleParking}的怠速状态.<br>
 * 输出：统计
 * @author 沈东生
 */
public class RangeTorque extends AbstractArithmetic<VarRangeTorque> {
    @Override
    public void calculate() {
        logger.debug("进入发动机扭矩区间算法.");
        CommonData commonData = getCommonData();
        int actualEnginePercentTor = commonData.getEngineOutputTorque();

        if (!RangeTorqueEnum.RANGE_VALID.getRange().contains(actualEnginePercentTor)) {
            return;
        }

        VarRangeTorque var = getObj();
        int duration = commonData.getDffTime();
        if ( commonData.isWork()) {
            if (RangeTorqueEnum.RANGE_0.getRange().contains(actualEnginePercentTor)) {
                var.setTorque0Duration(
                        Optional.ofNullable(var.getTorque0Duration()).orElse(0) + duration);
            } else if (RangeTorqueEnum.RANGE_10.getRange().contains(actualEnginePercentTor)) {
                var.setTorque10Duration(
                        Optional.ofNullable(var.getTorque10Duration()).orElse(0)  + duration);
            } else if (RangeTorqueEnum.RANGE_20.getRange().contains(actualEnginePercentTor)) {
                var.setTorque20Duration(
                        Optional.ofNullable(var.getTorque20Duration()).orElse(0) + duration);
            } else if (RangeTorqueEnum.RANGE_30.getRange().contains(actualEnginePercentTor)) {
                var.setTorque30Duration(
                        Optional.ofNullable(var.getTorque30Duration()).orElse(0)  + duration);
            } else if (RangeTorqueEnum.RANGE_40.getRange().contains(actualEnginePercentTor)) {
                var.setTorque40Duration(
                        Optional.ofNullable(var.getTorque40Duration()).orElse(0) + duration);
            } else if (RangeTorqueEnum.RANGE_50.getRange().contains(actualEnginePercentTor)) {
                var.setTorque50Duration(
                        Optional.ofNullable(var.getTorque50Duration()).orElse(0)  + duration);
            } else if (RangeTorqueEnum.RANGE_60.getRange().contains(actualEnginePercentTor)) {
                var.setTorque60Duration(
                        Optional.ofNullable(var.getTorque60Duration()).orElse(0)  + duration);
            } else if (RangeTorqueEnum.RANGE_70.getRange().contains(actualEnginePercentTor)) {
                var.setTorque70Duration(
                        Optional.ofNullable(var.getTorque70Duration()).orElse(0)  + duration);
            } else if (RangeTorqueEnum.RANGE_80.getRange().contains(actualEnginePercentTor)) {
                var.setTorque80Duration(
                        Optional.ofNullable(var.getTorque80Duration()).orElse(0) + duration);
            } else if (RangeTorqueEnum.RANGE_90.getRange().contains(actualEnginePercentTor)) {
                var.setTorque90Duration(
                        Optional.ofNullable(var.getTorque90Duration()).orElse(0) + duration);
            } else if (RangeTorqueEnum.RANGE_99.getRange().contains(actualEnginePercentTor)) {
                var.setTorque99Duration(
                        Optional.ofNullable(var.getTorque99Duration()).orElse(0)  + duration);
            } else if (RangeTorqueEnum.RANGE_100.getRange().contains(actualEnginePercentTor)) {
                var.setTorque100Duration(
                        Optional.ofNullable(var.getTorque100Duration()).orElse(0) + duration);
            }
        }

        if (var.getMaxTorque() == null || actualEnginePercentTor > var.getMaxTorque()) {
            var.setMaxTorque(actualEnginePercentTor);
        }
        if (var.getMinTorque() == null || actualEnginePercentTor < var.getMinTorque()) {
            var.setMinTorque(actualEnginePercentTor);
        }

        var.setTorqueSum(Optional.ofNullable(var.getTorqueSum()).orElse(0L) + actualEnginePercentTor);
        var.setTorqueCount(Optional.ofNullable(var.getTorqueCount()).orElse(0) + 1);
        logger.debug("离开发动机扭矩区间算法.");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarRangeTorque var = getObj();
        //torque
        statisticDataOut.setTorque0Duration(ArithmeticUtils.timeMsToSecond(var.getTorque0Duration()));
        statisticDataOut.setTorque10Duration(ArithmeticUtils.timeMsToSecond(var.getTorque10Duration()));
        statisticDataOut.setTorque20Duration(ArithmeticUtils.timeMsToSecond(var.getTorque20Duration()));
        statisticDataOut.setTorque30Duration(ArithmeticUtils.timeMsToSecond(var.getTorque30Duration()));
        statisticDataOut.setTorque40Duration(ArithmeticUtils.timeMsToSecond(var.getTorque40Duration()));
        statisticDataOut.setTorque50Duration(ArithmeticUtils.timeMsToSecond(var.getTorque50Duration()));
        statisticDataOut.setTorque60Duration(ArithmeticUtils.timeMsToSecond(var.getTorque60Duration()));
        statisticDataOut.setTorque70Duration(ArithmeticUtils.timeMsToSecond(var.getTorque70Duration()));
        statisticDataOut.setTorque80Duration(ArithmeticUtils.timeMsToSecond(var.getTorque80Duration()));
        statisticDataOut.setTorque90Duration(ArithmeticUtils.timeMsToSecond(var.getTorque90Duration()));
        statisticDataOut.setTorque99Duration(ArithmeticUtils.timeMsToSecond(var.getTorque99Duration()));
        statisticDataOut.setTorque100Duration(ArithmeticUtils.timeMsToSecond(var.getTorque100Duration()));

        statisticDataOut.setTorqueSum(var.getTorqueSum());
        statisticDataOut.setTorqueCount(var.getTorqueCount());

        statisticDataOut.setMaxTorque(var.getMaxTorque());
        statisticDataOut.setMinTorque(var.getMinTorque());

    }
}
